home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / progtool / modula2 / modprint / compare.fix next >
Encoding:
Text File  |  1994-09-22  |  1.2 KB  |  59 lines

  1.  
  2. PROCEDURE Compare ( First, Second : ARRAY OF CHAR ) : CompareResults;
  3.  
  4. VAR
  5.   SizeFirst, SizeSecond, SearchSize : CARDINAL;
  6.   Index : CARDINAL;
  7.   FirstBigger : BOOLEAN;
  8.  
  9. BEGIN
  10.   SizeFirst := Length ( First );
  11.   SizeSecond := Length ( Second );
  12.   FirstBigger := (SizeFirst >= SizeSecond);
  13.  
  14. (* If you can hack these boundary conditions *)
  15. (* better than this brute force method,      *)
  16. (* try it!  I would like to see it!          *)
  17.   IF SizeFirst = 0 THEN
  18.     IF FirstBigger THEN
  19.       RETURN Equal
  20.     ELSE 
  21.       RETURN Less
  22.     END
  23.   ELSIF SizeSecond = 0 THEN
  24.     RETURN Greater
  25.   END;
  26.  
  27.   IF FirstBigger THEN
  28.     SearchSize := SizeSecond;
  29.   ELSE
  30.     SearchSize := SizeFirst;
  31.   END;
  32.  
  33.   Index := 0;
  34.  
  35.   WHILE ( Index < SearchSize ) DO
  36.     IF First[Index] = Second[Index] THEN
  37.         INC (Index);
  38.     ELSIF First[Index] > Second[Index] THEN
  39.     RETURN Greater;
  40.     ELSE RETURN Less;
  41.     END; (* IF *)
  42.   END;
  43.  
  44.  (*
  45.   * At this point, Index = SearchSize and both strings are equal up
  46.   * to Index-1.  The longer string must be greater.
  47.   *)
  48.   IF ( SizeFirst ) > ( Index ) THEN
  49.     RETURN Greater;
  50.   ELSIF (SizeSecond ) > ( Index ) THEN
  51.     RETURN Less;
  52.   ELSE 
  53.     RETURN Equal;
  54.   END;
  55.  
  56. END Compare;
  57.  
  58.  
  59.